using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JVIT.TimeSheet.BLL;

namespace JVIT.TimeSheet.GUI
{
    public partial class WorkItemDetail : Form
    {
        private WorkItem WorkItem { get; set; }
        private DateTime WorkDate { get; set; }

        public WorkItemDetail(WorkItem workItem)
        {
            InitializeComponent();
            this.WorkItem = workItem;
            this.WorkDate = workItem.Started == null || workItem.Started.Value == null ? DateTime.Now : workItem.Started.Value;
            Bind();
        }
        private void Bind()
        {
            txtDescription.Text = this.WorkItem.Description;
            txtFrom.Text = this.WorkItem.Started.ToReadableString();
            txtTo.Text = this.WorkItem.Ended.ToReadableString();
            List<StartedEndedDuration> startedEndedDurations = (from Pause pause in this.WorkItem.Pauses
                                                                select new StartedEndedDuration()
                                                                {
                                                                    Started = pause.Started.ToReadableString(),
                                                                    Ended = pause.Ended.ToReadableString(),
                                                                    Duration = pause.DurationUnfinished.ToReadableString(),
                                                                    PauseItem = pause
                                                                }
                                                               ).ToList();
            dgvPauses.DataSource = startedEndedDurations;
        }
        private void Save()
        {
            this.WorkItem.Description = txtDescription.Text;
            this.WorkItem.Started = GetDateTimeFromTimeString(txtFrom.Text);
            this.WorkItem.Ended = GetDateTimeFromTimeString(txtTo.Text);
        }
        private new List<string> Validate()
        {
            List<string> result = new List<string>();
            if (string.IsNullOrEmpty(txtDescription.Text))
                result.Add("Description mandatory");
            if (GetDateTimeFromTimeString(txtFrom.Text) == null)
                result.Add("Valid From time mandatory");
            if (string.IsNullOrEmpty(txtTo.Text) == false && GetDateTimeFromTimeString(txtTo.Text) != null && GetDateTimeFromTimeString(txtFrom.Text) != null && GetDateTimeFromTimeString(txtTo.Text) <= GetDateTimeFromTimeString(txtFrom.Text))
                result.Add("End time should be after Start time");
            return result;
        }
        private bool HandleValidation()
        {
            List<string> validationResult = Validate();
            if (validationResult.Count > 0)
            {
                MessageBox.Show(string.Join(Environment.NewLine, validationResult.ToArray()));
                return false;
            }
            return true;
        }
        private DateTime? GetDateTimeFromTimeString(string value)
        {
            DateTime? result = null;
            if (string.IsNullOrEmpty(value) == false)
            {
                try
                {
                    string[] timeParts = value.Split(':');
                    result = new DateTime(this.WorkDate.Year, this.WorkDate.Month, this.WorkDate.Day, int.Parse(timeParts[0]), int.Parse(timeParts[1]), int.Parse(timeParts[2]));
                }
                catch { }
            }
            return result;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (HandleValidation())
            {
                Save();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void dgvPauses_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            if (dgv.SelectedRows.Count > 0)
            {
                StartedEndedDuration item = dgv.SelectedRows[0].DataBoundItem as StartedEndedDuration;
                if (item != null && item.PauseItem != null)
                {
                    Pause pause = item.PauseItem as Pause;
                    if (pause != null)
                    {
                        PauseDetail pauseDetail = new PauseDetail(pause);
                        if (pauseDetail.ShowDialog() == DialogResult.OK)
                        {
                            //MessageBox.Show("Save changes!");
                            Save();
                        }
                    }
                }
            }
        }
    }
    public class StartedEndedDuration
    {
        public string Started { get; set; }
        public string Ended { get; set; }
        public string Duration { get; set; }
        [Browsable(false)]
        public Pause PauseItem { get; set; }
    }
}
